package org.tnc.budget.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.tnc.budget.model.BudgetCenter;

/**
 * 
 * @author houjj | 2011-4-4 下午04:02:52
 */
public class BudgetCenterDaoHibernate extends
		GenericHibernateDAO<BudgetCenter, Integer> {

	/**
	 * 
	 * @param bcValid 为true表示只查询有效的，为false表示查询所有的记录
	 * @param vacation null表示查询全部，true表示仅查询假期，false表示不查询假期。
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BudgetCenter> getValidBudgetCenterList(boolean bcValid, Boolean vacation) {
		DetachedCriteria c = DetachedCriteria.forClass(BudgetCenter.class);
		if (bcValid)
			c.add(Restrictions.eq("bcValid", bcValid));
		
		if (vacation != null) {
			if (!vacation)
				c.add(Restrictions.eq("vacation", false));
			else
				c.add(Restrictions.eq("vacation", true));
		}
		
		c.addOrder(Order.desc("bcDate"));

		List<BudgetCenter> list = new ArrayList<BudgetCenter>();
		list = this.findByCriteria(c);
		return list;
	}

	/**
	 * 
	 * @param budgetName
	 * @return
	 */
	public BudgetCenter getBudgetByName(String budgetName) {
		DetachedCriteria c = DetachedCriteria.forClass(BudgetCenter.class);
		c.add(Restrictions.eq("bcName", budgetName));

		List<BudgetCenter> list = this.findByCriteria(c);
		BudgetCenter bc = null;
		if (list != null && list.size() > 0)
			bc = list.get(0);
		return bc;
	}

	/**
	 * 预算中心的编号，非记录ID
	 * 
	 * @param budgetId
	 * @return
	 */
	public BudgetCenter getBudgetByBcId(String budgetId) {
		DetachedCriteria c = DetachedCriteria.forClass(BudgetCenter.class);
		c.add(Restrictions.eq("bcId", budgetId));

		List<BudgetCenter> list = this.findByCriteria(c);
		BudgetCenter bc = null;
		if (list != null && list.size() > 0)
			bc = list.get(0);
		return bc;
	}
}
